package com.base.myproject.client.ui.celleditor;
/**
 * 辅助录入和渲染
 */
import com.base.myproject.client.busi.BaseStore;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.LoadListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;

public class BaseCellEditorCB extends CellEditor implements GridCellRenderer{
	BaseComboBox combobox = null;
	final java.util.Map<String, String> ht = new java.util.HashMap<String, String>();
	final String key;
	final String htvalue;
	BaseStore store;
	String[] sourcecol;
	String[] objectcol;
	public BaseCellEditorCB(BaseComboBox field,final String key,final String value) {
		super(field);
		this.key = key;
		this.htvalue = value;
		 combobox = field;
		 combobox.getStore().getLoader().addLoadListener(new LoadListener(){
			 public void loaderLoad(LoadEvent le) {
				 for (int i = 0; i < combobox.getStore().getModels().size(); i++) {
						ht.put(((ModelData)combobox.getStore().getModels().get(i)).get(key).toString(), ((ModelData)combobox.getStore().getModels().get(i)).get(value)
								.toString());
   
					}
			  }
		 });
	
	}

	public Object preProcessValue(Object value) {

		if (value == null) {
			return value;
		}
		
		return combobox.findModel(value.toString(), this.htvalue, "");
		// return combobox.getValue();

	}

	@Override
	public Object postProcessValue(Object value) {
		if (value == null) {

			return value;
		}
	
		dopost(combobox.getValue());

		return combobox.getValue().get(this.htvalue);

	}

	@Override
	public Object render(ModelData model, String property, ColumnData config,
			int rowIndex, int colIndex, ListStore store, Grid grid) {
		// TODO Auto-generated method stub
		if(ht.containsKey(model.get(property)))
		return ht.get(model.get(property));
		else 
			return "zz";
	
	}
	
	/**
	 * @return the store
	 */
	public BaseStore getStore() {
		return store;
	}

	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore store) {
		this.store = store;
	}

	public void dopost(final ModelData modeldata)
	{
		
		//store.setValue("qty", modeldata.get("qtyunit"));
		for(int i=0;i<sourcecol.length;i++)
		{
			store.setValue(objectcol[i], modeldata.get(sourcecol[i]));
		}
	}

	/**
	 * @return the sourcecol
	 */
	public String[] getSourcecol() {
		return sourcecol;
	}

	/**
	 * @param sourcecol the sourcecol to set
	 */
	public void setSourcecol(String[] sourcecol) {
		this.sourcecol = sourcecol;
	}

	/**
	 * @return the objectcol
	 */
	public String[] getObjectcol() {
		return objectcol;
	}

	/**
	 * @param objectcol the objectcol to set
	 */
	public void setObjectcol(String[] objectcol) {
		this.objectcol = objectcol;
	}
	
}
